/**
 * App模块 - 管理应用级别的状态
 */

const state = {
  // 应用标题
  title: '宠物管理系统',
  // 应用版本
  version: '1.0.0',
  // 侧边栏状态
  sidebarCollapsed: false,
  // 加载状态
  loading: false,
  // 设备类型
  device: 'desktop', // desktop, tablet, mobile
  // 语言设置
  language: 'zh-CN'
}

const mutations = {
  // 设置应用标题
  SET_TITLE(state, title) {
    state.title = title
    document.title = title
  },
  
  // 切换侧边栏状态
  TOGGLE_SIDEBAR(state) {
    state.sidebarCollapsed = !state.sidebarCollapsed
  },
  
  // 设置侧边栏状态
  SET_SIDEBAR(state, collapsed) {
    state.sidebarCollapsed = collapsed
  },
  
  // 设置加载状态
  SET_LOADING(state, loading) {
    state.loading = loading
  },
  
  // 设置设备类型
  SET_DEVICE(state, device) {
    state.device = device
  },
  
  // 设置语言
  SET_LANGUAGE(state, language) {
    state.language = language
  }
}

const actions = {
  // 设置应用标题
  setTitle({ commit }, title) {
    commit('SET_TITLE', title)
  },
  
  // 切换侧边栏
  toggleSidebar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  
  // 设置加载状态
  setLoading({ commit }, loading) {
    commit('SET_LOADING', loading)
  },
  
  // 检测设备类型
  detectDevice({ commit }) {
    const width = window.innerWidth
    let device = 'desktop'
    
    if (width < 768) {
      device = 'mobile'
    } else if (width < 1024) {
      device = 'tablet'
    }
    
    commit('SET_DEVICE', device)
  },
  
  // 切换语言
  changeLanguage({ commit }, language) {
    commit('SET_LANGUAGE', language)
    // 这里可以添加国际化逻辑
    localStorage.setItem('language', language)
  }
}

const getters = {
  // 获取完整的页面标题
  pageTitle: (state) => (subtitle) => {
    return subtitle ? `${subtitle} - ${state.title}` : state.title
  },
  
  // 是否为移动设备
  isMobile: (state) => state.device === 'mobile',
  
  // 是否为平板设备
  isTablet: (state) => state.device === 'tablet',
  
  // 是否为桌面设备
  isDesktop: (state) => state.device === 'desktop'
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}